package service

import (
	"beego-admin/library"
	"beego-admin/models"
	"github.com/astaxie/beego"
	"strings"
)

type ArticleObj struct{
	Id                 int    `json:"id"`
	Title				string `json:"title"`
	Introduction        string `json:"introduction"`
	ImageUrl				string `json:"image_url"`
	Author				string `json:"author"`
	CatName               string `json:"cat_name"`
	TagsName               string `json:"tags_name"`
	AddTime                string `json:"add_time"`
}

func ArticleListFormat(articleList *[]models.ArticleObj) []ArticleObj{
	imageHost := beego.AppConfig.String("qiniu::imageHost")
	var articleData []ArticleObj
	for _,v := range *articleList{
		catName := ""
		if v.CatId > 0{
			articleCatInfo,_ := models.GetArticleCatInfoById(v.CatId)
			catName = articleCatInfo.CatName
		}

		tagsName := ""
		if v.Tags != ""{
			tagsArr := strings.Split(v.Tags, ",")
			articleCatList,_ := models.GetArticleTagsListByIds(library.String2Int(tagsArr))
			for _,v := range *articleCatList{
				tagsName += v.TagsName + ","
			}
			tagsName = tagsName[0:len(tagsName)-1]
		}

		authorName := ""
		if v.Author > 0{
			adminUserInfo,_ := models.GetAdminUserInfoById(v.Author)
			authorName = adminUserInfo.AdminUser
		}
		addTime := v.AddTime.Format("2006-01-02 15:04:05")
		tmpParam := ArticleObj{v.Id,v.Title,v.Introduction,imageHost + v.ImageUrl,authorName,catName,tagsName,addTime}
		articleData = append(articleData, tmpParam)
	}
	return articleData
}
